<?php
class ControllerCommonTool extends Controller {
    private $error = array();
    
    public function sendMsg()
    {
        if (! $this->actionCheck(false, false)) {
            return;
        }
        if($this->validate()){
            $this->load->model('tool/messager');
            $code = '0000' . rand(0, 9999);
            $code = substr($code, strlen($code) - 4);
            if(isset($this->session->data['phone_valide_code'])){
                $code = $this->session->data['phone_valide_code'];
            }
            if($this->model_tool_messager->sendCode($this->request->get['phone'],$code))
            {
                $this->session->data['phone_valide_code'] = $code;
                $this->response->setDataResult(1,"",null);
                return;
            }
            else
            {
                $this->response->setDataResult(0,"短信服务异常，请稍后重试",null);
                return;
            }
        }
        if (count($this->error) > 0) {
            $error_warning = array_shift($this->error);
        } else {
            $error_warning = '';
        }
        $this->response->setDataResult(0, $error_warning, null);
        return;
    }
    
    public function codeCheck()
    {
        if ($this->validate()) {
            $this->response->setDataResult(1,"",null);
        } else {
            $this->response->setDataResult(0,"",null);
        }
    }
    
    private function validate()
    {
        if (! preg_match('/^1\d{10}$/', $this->request->get['phone'])) {
            $this->error['phone'] = '手机号不合法！';
        }
        return !$this->error;
    }
}